﻿using System;
using System.Collections.Generic;
using System.Text;

namespace AGuang.WebTool {
    /// <summary>
    /// JSON字符串创建
    /// </summary>
    [Obsolete("已被最新的类,JSON替代")]
    public class JSONCreater {
        private List<String> list = new List<string>();

        /// <summary>
        /// 输出最终字符串
        /// </summary>
        /// <returns></returns>
        public override String ToString() {
            String[] strs = list.ToArray();
            return "{" + String.Join(",", strs) + "}";
        }

        /// <summary>
        /// 将json中添加一个字符类型的键值
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="value">值</param>
        public void Add(String key, String value) {
            String item = key + ":\"" + value.Replace("\"", "\\\"") + "\"";
            list.Add(item);
        }

        /// <summary>
        /// 将json中添加一个JSONCreater类型的键值
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="value">值</param>
        public void Add(String key, JSONCreater json) {
            String item = key + ":" + json.ToString();
            list.Add(item);
        }

        /// <summary>
        /// 将json中添加一个整型的键值
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="value">值</param>
        public void Add(String key, int value) {
            String item = key + ":" + value;
            list.Add(item);
        }

        /// <summary>
        /// 将json中添加一个长整类型的键值
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="value">值</param>
        public void Add(String key, long value) {
            String item = key + ":" + value;
            list.Add(item);
        }

        /// <summary>
        /// 将json中添加一个双精度类型的键值
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="value">值</param>
        public void Add(String key, double value) {
            String item = key + ":" + value;
            list.Add(item);
        }

        /// <summary>
        /// 将json中添加一个单精度类型的键值
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="value">值</param>
        public void Add(String key, Single value) {
            String item = key + ":" + value;
            list.Add(item);
        }

        /// <summary>
        /// 将json中添加一个布尔类型的键值
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="value">值</param>
        public void Add(String key, bool value) {
            String item = key + ":" + value.ToString().ToLower();
            list.Add(item);
        }

        /// <summary>
        /// 将json中添加一个数组类型的键值
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="value">值</param>
        public void Add(String key, Array array) {
            String[] strs = new String[array.Length];
            if (array.GetType() == typeof(String[])) {
                for (int i = 0; i < array.Length; i++) {
                    strs[i] = "\"" + array.GetValue(i).ToString().Replace("\"", "\\\"") + "\"";
                }
            } else {
                for (int i = 0; i < array.Length; i++) {
                    strs[i] = array.GetValue(i).ToString();
                }
            }
            String item = key+":[" + String.Join(",", strs) + "]";
            list.Add(item);
        }

        public void Add(String key, ArrayJSON value) {
            String item = key + ":" + value.ToString();
            list.Add(item);
        }

        /// <summary>
        /// 将数组类型json输出为最终字符串
        /// </summary>
        /// <param name="jsons">JSONCreater类型的数组</param>
        /// <returns></returns>
        public static String ToString(JSONCreater[] jsons) {
            String[] list = new string[jsons.Length];
            for (int i = 0; i < jsons.Length; i++) {
                list[i] = jsons[i].ToString();
            }
            return "[" + String.Join(",", list) + "]";
        }
    }
    
    public class ArrayJSON {
        private List<String> list = new List<string>();
        public void Add(String value) {
            list.Add("\"" + value + "\"");
        }

        public void Add(ArrayJSON value) {
            list.Add(value.ToString());
        }

        public void Add(JSONCreater value) {
            list.Add(value.ToString());
        }

        public String ToString() {
            String[] strs = list.ToArray();
            return "[" + String.Join(",", strs) + "]";
        }
    }
}
